<!-- 广场 -->
<template>
	<view class="square-item">
		<view class="head-info">
			<view class="head-info-left" v-if="props.isDynamic" @click="getDynamic">
				<HeadImg :headAvatar="props.dataItem.UserInfo.Avatar" :RealState="props.dataItem.UserInfo.RealState"
					:imgWidth="88" />
				<view class="square-item-head-title">
					<view class="square-item-head-name">
						<text>{{ props.dataItem.UserInfo.ShowName }}</text>
						<Ident :userInfo="props.dataItem.UserInfo"></Ident>
					</view>
					<!-- <text class="send-timer">{{ props.dataItem.AddTime}}</text> -->
					<text class="send-timer">{{ timerFn(props.dataItem.AddTime)}}</text>
				</view>
			</view>
			<view class="head-info-left head-info-left-date" v-else>
				<view class="date-box-top">
					<text>{{ timerYMDFn(props.dataItem.TimesTamp, 3) }}</text>
					<text>/{{ timerYMDFn(props.dataItem.TimesTamp, 2) }}</text>
				</view>
				<view class="date-box-bottom">
					{{ timerYMDFn(props.dataItem.TimesTamp, 1) }}
				</view>
			</view>
			<view class="head-info-right" @click="squareFn()">
				<img src="@/assets/image/icon11.png" alt="">
			</view>
		</view>
		<view class="square-item-content">
			<van-text-ellipsis rows="2" :content="props.dataItem.Text" expand-text="展开" collapse-text="收起" />
		</view>
		<DynamicImg :imgList="props.dataItem.Files" :itemId="props.dataItem.ID" />
		<view class="square-item-label-box" v-if="props.isDynamic">
			<view class="square-item-label" v-for="item in props.dataItem.Topics" :key="item.ID">
				<img src="@/assets/image/icon14.png" alt="">
				<text>{{ item.Title }}</text>
			</view>
		</view>
		<view class="square-item-address-box" v-if="props.isDynamic && props.dataItem.LocationText">
			<img src="@/assets/image/icon03.png" alt="">
			<text>{{ props.dataItem.LocationText }}</text>
		</view>
		<view class="square-item-chat-box">
			<view class="square-item-chat-left" v-if="props.isDynamic" @click="privateChatFn(props.dataItem.UserInfo)">
				<img src="@/assets/image/icon15.png" alt="">
			</view>
			<view class="square-item-chat-left" v-else></view>
			<view class="square-item-chat-right">
				<view class="square-item-chat-right-item" @click="praiseFn(props.dataItem.ID)">
					<img src="@/assets/image/icon16.png" alt="" v-if="props.dataItem.IsLike">
					<img src="@/assets/image/icon18.png" alt="" v-else>
					<text>{{ props.dataItem.LikeCount ? props.dataItem.LikeCount : '点赞' }}</text>
				</view>
				<view class="square-item-chat-right-item" @click="reviewFn(props.dataItem.ID)">
					<img src="@/assets/image/icon17.png" alt="">
					<text>{{ props.dataItem.CommentCount ? props.dataItem.CommentCount : '评论' }}</text>
				</view>
			</view>
		</view>
	</view>
	<ActionSheetBox :actionsArr="state.actions" :currentItem="props.dataItem" ref="ActionSheetRef" />
</template>

<script setup>
	import {
		reactive,
		ref,
		getCurrentInstance,
		onMounted,
		provide
	} from "vue"
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import DynamicImg from "@/components/dynamicImg/index.vue"

	import {
		getAuthorization,
		setAuthorization
	} from "@/utils/auth.js"

	let {
		proxy
	} = getCurrentInstance()

	const emits = defineEmits(['on-praise'])

	const state = reactive({
		squareList: [],
		actions: [{
				name: '删除'
			},
			{
				name: '仅自己可见'
			},
		],
		show: false,
		isPerson: ""
	})

	onLoad((options) => {
		state.isPerson = options.isPerson
		if (!options.isPerson) {
			state.actions = [{
					name: '不喜欢'
				},
				// { name: '举报123' },
			]
		}
	})

	const props = defineProps({
		isDynamic: {
			type: Boolean,
			default: true
		},
		dataItem: {
			type: Object,
			default: () => {}
		}
	})

	// 输出年月日 (传入 date 对象)
	const becomeYMD = function(dateObj) {
		return `${dateObj.getFullYear()}-${zeroFill(dateObj.getMonth() + 1)}-${zeroFill(dateObj.getDate())}`
	}
	// 补零操作 (传入 number 类型的数字，返回 string 类型)
	const zeroFill = function(num) {
		return num < 10 ? '0' + num : '' + num;
	}
	// 格式化时间 (返回示例: xx分钟前、xx小时前、xx天前、2020-01-01)
	const timerFn = (time) => {
		const nTime = new Date();
		const nTimeStamp = nTime.getTime(); // 当前时间的毫秒数
		const fTime = new Date(time);
		time = fTime.getTime();
		const nTime_YMD = becomeYMD(nTime);
		const fTime_YMD = becomeYMD(fTime);
		// 判断是否在24小时内
		if (nTimeStamp - time < 86400000) {
			// 一小时之外
			if ((nTimeStamp - time) > 3600000) {
				let nTime_hours = nTime.getHours();
				let fTime_hours = fTime.getHours();
				if (fTime_hours >= nTime_hours) {
					return 24 - fTime_hours + nTime_hours + '小时前';
				} else {
					return nTime_hours - fTime_hours + '小时前';
				}
			}
			// 是一小时之内
			else {
				let n_min = nTime.getMinutes();
				let f_min = fTime.getMinutes();
				let minute = n_min - f_min;
				if (minute === 0) {
					return '现在';
				}
				if (minute > 0) {
					return minute + '分钟前';
				} else {
					return minute + 60 + '分钟前';
				}
			}
		} else {
			// 是最近30天
			if (
				nTimeStamp - time < 2592000000
			) {
				if (
					becomeYMD(new Date(nTimeStamp - 86400000)) == fTime_YMD
				) {
					return '昨天';
				} else {
					return Math.floor(
						(nTimeStamp - time) / (24 * 60 * 60 * 1000)
					) + '天前';
				}
			}
			// 不是最近30天
			else {
				return fTime_YMD;
			}
		}
	}

	let ActionSheetRef = ref()
	const squareFn = () => {
		ActionSheetRef.value.actionSheetShow()
	}

	const getDynamic = () => {
		uni.navigateTo({
			url: '/pages/home/person/dynamic/index?userId=' + props.dataItem.UserInfo.ID
		})
	}

	// 查看图片
	const photoView = () => {
		uni.navigateTo({
			url: '/pages/swriperPage/index'
		})
	}

	const reviewFn = (id) => {
		uni.navigateTo({
			url: '/pages/reviewPage/index?id=' + id
		})
	}

	// 点赞
	import {
		likeTo
	} from "@/api/list/index.js"

	const likeToFn = async (id) => {
		let params = {
			userId: getAuthorization("userId"),
			likeType: 0
		}
		try {
			let data = await proxy.$post(likeTo(id, params))
			if (data.code == 200) {
				uni.showToast({
					title: '点赞成功',
					icon: "none"
				})
				emits('on-praise', id)
			}
		} catch (error) {
			uni.showToast({
				title: error.message,
				icon: "none"
			})
		}

	}

	const praiseFn = (id) => {
		likeToFn(id)
	}

	// 进入私聊
	const privateChatFn = (item) => {
		uni.navigateTo({
			url: '/pages/privateChat/index?userIMId=' + item.IMId
		})
	}

	// 获取事件
	const timerYMDFn = (date, types) => {
		let _date = new Date(date * 1000)
		let Y = _date.getFullYear()
		let M = _date.getMonth() + 1 < 10 ? "0" + (_date.getMonth() + 1) : _date.getMonth() + 1
		let D = _date.getDate() + 1 < 10 ? "0" + _date.getDate() : _date.getDate()
		if (types == 1) return Y
		if (types == 2) return M
		if (types == 3) return D
	}
</script>

<style lang="scss" scoped>
	.square-item {
		margin: 32rpx 20rpx 20rpx 20rpx;
	}

	.head-img {
		width: 88rpx;
		height: 88rpx;
		// border: 1px solid #e2e2e2;
	}

	.head-info {
		display: flex;
		align-items: center;
		justify-content: space-between;

		.head-info-left {
			display: flex;
			align-items: center;

			.square-item-head-title {
				margin-left: 20rpx;

				.square-item-head-name {
					font-size: 42rpx;
					color: #262626;
					height: 32rpx;
					display: flex;
					align-items: center;

					img {
						height: 32rpx;
						margin-left: 10rpx;
					}

					text {
						flex: none;
						font-size: 32rpx;
					}
				}

				.send-timer {
					display: block;
					color: #999;
					margin-top: 10rpx;
					font-size: 24rpx;
				}
			}
		}

		.head-info-right {
			img {
				height: 32rpx;
			}
		}
	}

	.square-item-content {
		margin: 24rpx 0;
	}

	:deep(.van-text-ellipsis__action) {
		color: #333;
	}

	.square-item-img {
		.square-item-img-item {
			border-radius: 16rpx;
			overflow: hidden;
			display: block;
		}
	}

	.square-item-img1 {
		.square-item-img-item {
			width: 50%;
		}
	}

	.square-item-img2 {
		display: flex;

		.square-item-img-item {
			width: calc(50% - 7rpx);
			height: 384rpx;
		}

		.square-item-img-item:nth-child(1) {
			margin-right: 14rpx;
		}
	}

	.square-item-img3 {
		position: relative;
		display: block;

		.square-item-img-item:nth-child(1) {
			width: 468rpx;
			height: 468rpx;
		}

		.square-item-img-item:nth-child(2),
		.square-item-img-item:nth-child(3) {
			width: 214rpx;
			height: 214rpx;
			position: absolute;
			top: 0;
			right: 0;
		}

		.square-item-img-item:nth-child(3) {
			top: auto;
			bottom: 0;
		}

		.square-item-img-tips {
			position: absolute;
			background: rgba(0, 0, 0, .5);
			color: #fff;
			right: 16rpx;
			bottom: 16rpx;
			display: flex;
			align-items: center;
			padding: 4rpx 8rpx;

			img {
				width: 20rpx
			}

			text {
				margin-left: 5rpx;
			}
		}
	}

	.square-item-label-box {
		display: flex;
		flex-wrap: wrap;

		.square-item-label {
			color: #666;
			display: flex;
			align-items: center;
			font-size: 24rpx;
			margin-top: 16rpx;
			margin-right: 20rpx;
			flex: none;

			img {
				width: 32rpx;
				height: 32rpx;
				margin-right: 5rpx;
			}
		}
	}

	.square-item-address-box {
		display: flex;
		align-items: center;
		font-size: 20rpx;
		color: #0036F6;
		margin-top: 24rpx;

		img {
			width: 32rpx;
			height: 32rpx;
			margin-right: 10rpx;
		}
	}

	.square-item-chat-box {
		display: flex;
		align-items: center;
		margin-top: 24rpx;
		justify-content: space-between;

		img {
			display: block;
			width: 48rpx;
		}

		.square-item-chat-right {
			display: flex;

			.square-item-chat-right-item {
				img {
					margin-right: 10rpx;
				}

				display: flex;
				align-items:center;
				color:#999;
				margin-left: 72rpx;
				font-size: 28rpx;
			}

			.square-item-chat-right-item-cur {
				color: #262626;
			}
		}
	}

	.head-info-left-date {
		display: block !important;

		.date-box-top {
			text:nth-child(1) {
				font-size: 36rpx;
				font-weight: 600;
			}

			text:nth-child(2) {
				font-size: 24rpx;
			}
		}

		.date-box-bottom {
			margin-top: 6rpx;
			font-size: 24rpx;
		}
	}
</style>
